<?php

namespace app\admin\traits;

/**
 * 统计
 * Trait StatisticsTrait
 * @package app\admin\traits
 */
trait TableChangeTrait
{

    /**
     * @param $data
     * @return array
     */
    private function columnAndRowSwitch($data): array
    {
        $temp = [];
        foreach ($data as $i => $day) {
            foreach ($day as $j => $item) {
                $index = $j;

                if (!isset($temp[$index])) {
                    $temp[$index] = [
                        'name' => $item['title'],
                        'datas' => [],
                    ];
                }

                $temp[$index]['datas'][$i] = [
                    'value' => $item['value'],
                ];
            }
        }

        return array_values($temp);
    }

    /**
     * @param $data
     * @return array
     */
    private function groupColumnAndRowSwitch($data): array
    {
        $temp = [];
        foreach ($data as $i => $day) {

            foreach ($day as $k => $group) {
                $length = count($group['data']);

                foreach ($group['data'] as $j => $item) {
                    $index = $k . ':' . $j;

                    if (!isset($temp[$index])) {
                        $temp[$index] = [
                            'name' => $group['title'] .'-'. $item['title'],
                            'group_name' => $group['title'],
                            'is_group_first' => $j === 0,
                            'group_length' => $length,
                            'datas' => [],
                        ];
                    }

                    $temp[$index]['datas'][$i] = [
                        'value' => $item['value'],
                    ];
                }

            }
        }

        return array_values($temp);
    }
}
